import { bindable, containerless } from 'aurelia-framework';

@containerless
export class EmTaskCreateModal {

    // Task, Story, Improvement, Feature, Issue
    types = [{
        label: '缺陷',
        value: 'Issue',
    }, {
        label: '新功能',
        value: 'Feature'
    }, {
        label: '任务',
        value: 'Task'
    }, {
        label: '改进',
        value: 'Improvement'
    }, {
        label: '故事',
        value: 'Story'
    }];

    // Highest, High, Medium, Low, Lowest;
    priorities = [{
        label: '最高',
        value: 'Highest',
    }, {
        label: '高',
        value: 'High'
    }, {
        label: '中等',
        value: 'Medium'
    }, {
        label: '低',
        value: 'Low'
    }, {
        label: '最低',
        value: 'Lowest'
    }];

    task = {
        project: '',
        type: 'Issue',
        title: '',
        priority: 'Medium',
        module: '',
        effectVersion: '',
        resolvedVersion: '',
        operator: '',
    };

    attached() {
        $(this.form).form({
            on: 'blur',
            inline: true,
            fields: {
                project: 'empty',
                title: 'empty',
            }
        });

        this.labelsOpts = {
            fullTextSearch: true,
            allowAdditions: true,
        };

        $(this.labelsRef).dropdown(this.labelsOpts);
    }

    show(opts) {
        _.extend(this, opts);
        this.emModal.show({
            autoDimmer: false,
            hideOnApprove: false
        });
    }

    showHandler() {
        // reset
        this._listMyProjects();
        this.fileUploadVm.clear();
    }

    _listByProject(pid) {
        this._listModulesByProject(pid);
        this._listVersionsByProject(pid);
        this._listLabelsByProject(pid);
        this.project = _.find(this.projects, { id: +pid });
        this.memmbers = this.project.members;
        this.task.operator = this.project.leader.username;
        this.task.title = '';
        this.descEditorVm.value = '';
        this.task.module = '';
        this.task.effectVersion = '';
        this.task.resolvedVersion = '';
        $(this.labelsRef).dropdown('clear');

    }

    initLabelsUI(last) {
        if (last) {
            _.defer(() => {
                $(this.labelsRef).dropdown().dropdown('clear').dropdown(this.labelsOpts);
            });
        }
    }

    projectChangeHandler(val) {
        if (val) {
            this._listByProject(val);
        }
    }

    _listLabelsByProject(pid) {
        this.labels = [];
    }

    _listMyProjects() {
        $.get('/admin/task/project/listMy', (data) => {
            if (data.success) {
                this.projects = data.data;
                if (this.projects.length > 0) {
                    this.task.project = this.projects[0].id;
                    this._listByProject(this.task.project);
                }
            } else {
                toastr.error(data.data);
            }
        });
    }

    _listModulesByProject(pid) {
        if (!pid) {
            return;
        }

        $.get('/admin/task/module/listByProject', {
            pid: pid,
        }, (data) => {
            if (data.success) {
                this.modules = data.data;
            } else {
                toastr.error(data.data);
            }
        });
    }

    _listVersionsByProject(pid) {
        if (!pid) {
            return;
        }

        $.get('/admin/task/version/listByProject', {
            pid: pid,
        }, (data) => {
            if (data.success) {
                this.versions = data.data;
            } else {
                toastr.error(data.data);
            }
        });
    }

    approveHandler(modal) {
        if (!$(this.form).form('is valid')) {
            toastr.error('表单验证不合法,请修改表单不合法输入!');
            return false;
        }

        modal.showDimmer();

        $.post('/admin/task/create', {
            pid: this.project.id,
            title: this.task.title,
            description: this.descEditorVm.value,
            modules: this.task.module,
            labels: $(this.labelsRef).dropdown('get value'),
            effectVersions: this.task.effectVersion,
            resolvedVersions: this.task.resolvedVersion,
            // epic: '', // Long
            operator: this.task.operator,
            priority: this.task.priority,
            // links: '',
            attachments: _.join(_.map(this.fileUploadVm.files, "id")),
            // parentTask: '', // Long

        }, (data) => {
            if (data.success) {
                modal.hide();
                toastr.success('任务创建成功!');
                ea.publish(nsCons.EVENT_TASK_CREATED, {
                    data: data.data
                });
            } else {
                modal.hideDimmer();
                toastr.error(data.data, '任务创建失败!');
            }
        });
    }
}
